package com.dtcnet.skmvp.mvp.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

import com.dtcnet.skmvp.base.BaseApplication;
import com.dtcnet.skmvp.mvp.presenter.IPresenter;
import com.dtcnet.skmvp.utils.MyToast;
import com.dtcnet.skmvp.widget.CustomProgressDialog;

import javax.inject.Inject;

public abstract class BaseActivity<P extends IPresenter, VB extends ViewBinding> extends AppCompatActivity implements IActivity<VB>, IView {

    @Inject
    protected P mPresenter;

    protected VB binding;

    // 简单的加载中弹出框
    private CustomProgressDialog mCPDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 将Activity添加到管理，方便退出
        getApp().addActivity(this);

        // 开始加载页面前校验数据
        if (!checkDataBefoAll()) {
            return;
        }

        // 通过Dagger注册数据
        if (getApplication() instanceof BaseApplication) {
            registerDagger(((BaseApplication) getApplicationContext()).getAppComponent());
        } else {
            registerDagger(null);
        }

        // 注册生命周期
        getLifecycle().addObserver(mPresenter);

        // 数据恢复用
        boolean doInitLoadData = dataRecovery(savedInstanceState);

        binding = viewBinding(getLayoutInflater());

        // 设置布局
        setContentView(binding.getRoot());

        initBindWidget();

        initSetListener();

        if (doInitLoadData) {
            initLoadData();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public boolean isSubThread() {
        return Looper.myLooper() != Looper.getMainLooper();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApp().removeActivity(this);
        if (mCPDialog != null && mCPDialog.isShowing()) {
            mCPDialog.dismiss();
        }
        mCPDialog = null;
        if (mPresenter != null) {
            mPresenter.onDestroy();
        }
        mPresenter = null;
    }

    public final <E extends BaseApplication> E getApp() {
        if (getApplication() instanceof BaseApplication) {
            return (E) getApplication();
        } else {
            throw new RuntimeException("无法获取-BaseApplication");
        }
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void toastInfo(String info) {
        // 判断是否在主线程刷新
        if (isSubThread()) {
            runOnUiThread(() -> MyToast.info(info));
        } else {
            MyToast.info(info);
        }
    }

    @Override
    public void toastSuccess(String success) {
        // 判断是否在主线程刷新
        if (isSubThread()) {
            runOnUiThread(() -> MyToast.success(success));
        } else {
            MyToast.success(success);
        }
    }

    @Override
    public void toastWarn(String warn) {
        // 判断是否在主线程刷新
        if (isSubThread()) {
            runOnUiThread(() -> MyToast.warn(warn));
        } else {
            MyToast.warn(warn);
        }
    }

    @Override
    public void toastError(String error) {
        // 判断是否在主线程刷新
        if (isSubThread()) {
            runOnUiThread(() -> MyToast.error(error));
        } else {
            MyToast.error(error);
        }
    }

    @Override
    public void startUseIntent(boolean needFinish, Class<?> acClass, Bundle bundle) {
        Intent intent = new Intent(this, acClass);

        if (bundle != null) {
            intent.putExtras(bundle);
        }

        startUseIntent(needFinish, intent);
    }

    @Override
    public void startUseIntent(boolean needFinish, Intent intent) {
        if (needFinish) {
            finish();
        }
        startActivity(intent);
    }

    @Override
    public void finishThis() {
        finish();
        getApp().removeActivity(this);
    }

    @Override
    public void exit() {
        getApp().exit();
    }

    @Override
    public void showLoading() {
        if (isSubThread()) {
            runOnUiThread(this::showLoading);
            return;
        }

        if (mCPDialog == null) {
            mCPDialog = new CustomProgressDialog(getContext());
        }
        mCPDialog.show();
    }

    @Override
    public void cancelLoading() {
        if (mCPDialog != null && mCPDialog.isShowing()) {
            // 判断是否在主线程
            if (isSubThread()) {
                runOnUiThread(() -> mCPDialog.dismiss());
            } else {
                mCPDialog.dismiss();
            }
        }
    }
}
